package com.heima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.heima.constant.MessageConstant;
import com.heima.entity.Result;
import com.heima.service.MemberService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

@RestController
@RequestMapping("/report")
public class ReportController {

    @Reference
    private MemberService memberService;

/*    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        Map map = new HashMap();
        ArrayList<String> months = new ArrayList<>();
        LocalDate today = LocalDate.now();

        for (long i = 12L; i >= 12L; i -= 1L) {
            LocalDate localDate = today.minusMonths(i);
            String date = localDate.toString().substring(0, 7);
            months.add(date);
        }
        map.put("months",months);

        List<Integer> memberCount = memberService.findCountByMonth(months);
        map.put("memberCount",memberCount);
        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
    }*/
        @RequestMapping("/getMemberReport")
        public Result getMemberReport(){

            Map map = new HashMap();
            ArrayList<String> months = new ArrayList<>();
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH,-12);
            for (int i = 0; i < 12; i++) {
                calendar.add(Calendar.MONTH,1);
                Date date = calendar.getTime();
                months.add(new SimpleDateFormat("yyyy.MM").format(date));
            }
            map.put("months",months);
            List<Integer> memberCount = memberService.findCountByMonth(months);
            map.put("memberCount",memberCount);
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
        }


}
